package com.cx.base.day19_thread;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Author: robert.song
 * @Date: Created in 2021/11/26
 */
public class Thread_13_6_原子性_原子字段更新器 {
    public static void main(String[] args) {
        Student student = new Student();
        AtomicReferenceFieldUpdater updater = AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");
        String name = (String) updater.get(student);
        System.out.println(updater.compareAndSet(student, name, "张三"));
        System.out.println(student);
    }
}

class Student {
    public volatile String name = "test";

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}